<?php
/**
 *  GMSDisplayer Plugin  
 *
 *  @license    GPL 2 ( http://www.gnu.org/licenses/gpl.html )
 *  @author     jaloma.ac [at] googlemail [dot] com
 *
 *  Description:
 *  Plugin to display GMSDisplayer with Java-Applet GMSDisplayer
 *
 *  Acknowledgements:
 *  The java applet author(s)
 */
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 
/**
 *  Panorama Plugin 
 */
class syntax_plugin_gmsdisplayer extends DokuWiki_Syntax_Plugin {

  var $dflt = array(
  'speed' => '5',
  'label' => 'jaloma',
  'bgcolor' => '255,255,255',
  'name' => 'gmsdisplay',
  'start' => 'on',
  'endless' => 'off'
); 
  /**
   *  Plugin Info 
   */
  function getInfo( ) {
    return array(
      'author' => 'Jürgen A. Lamers',
      'email'  => 'jaloma.ac [at] googlemail [dot] com',
      'date'   => '2008-10-19',
      'name'   => 'gmsdisplayer',
      'desc'   => 'GMSDisplayer Plugin <gmsdisplayer label="" speed="" bgcolor="" start="" endless="" name=""/>',
      'url'    => 'http://www.dokuwiki.org/plugin:gmsdisplayer',
    );
  }
 
  /**
   *  Typology?
   */  
  function getType( ) { 
		return 'substition'; 
	}
 
  /**
   *  Sort Code? 
   */  
  function getSort( ) {
    return 316;
  }
 
  /**
   *  Pattern Matching? 
   */  
  function connectTo($mode) {       
//    $this->Lexer->addSpecialPattern( '<gmsdisplayer.*?/>', $mode, 'plugin_gmsdisplayer' ); 
$this->Lexer->addSpecialPattern('<gmsdisplayer ?[^>\n]*>.*?</gmsdisplayer>',$mode,'plugin_gmsdisplayer');
  }
 
  /**
   *  Parsing
   *  1. Very rough parsing involved
   *  2. Applet parameters not included
   */    
  function handle( $match, $state, $pos, &$handler ) {
  	//123456789012
	//panorama
	//gmsdisplayer
    list($str_params,$str_points) = explode('>',substr($match,12,-14),-1);
    $gmap = $this->_extract_params($str_params);
    return $gmap;
  }
  /**
   * extract parameters for the gmsdisplay from the parameter string
   *
   * @param   string    $str_params   string of key="value" pairs
   * @return  array                   associative array of parameters key=>value
   */
  function _extract_params($str_params) {
    $param = array();
    preg_match_all('/(\w*)="(.*?)"/us',$str_params,$param,PREG_SET_ORDER);
    if (sizeof($param) == 0) {
      preg_match_all("/(\w*)='(.*?)'/us",$str_params,$param,PREG_SET_ORDER);
    }
    // parse match for instructions, break into key value pairs      
    $gmap = $this->dflt;
    foreach($param as $kvpair) {
      list($match,$key,$val) = $kvpair;
//      $key = strtolower($key);
      if (isset($gmap[$key])) $gmap[$key] = $val;        
    }

    return $gmap;
  }
 
 function getArchive() {
	return "gmsdisplayer.jar"; 	
 }
 function getCode() {
 	return "de.dokutransdata.gms.GMSDisplayer";
 }
  /**
   *  Rendering
   */  
  function render( $mode, &$renderer, $data ) {
    if($mode == 'xhtml'){
    	$lbl = $data['label'];
		$bgcolor = $data['bgcolor'];
		$speed = $data['speed'];
		$start = $data['start'];
		$endless = $data['endless'];
	  $width = 72;
	  $height = 75;
	  $name = $data['name'];
	  $archive = $this->getArchive();
	  $code = $this->getCode();
      $archive = DOKU_BASE . 'lib/exe/fetch.php?media=' . $archive;
	  $bname = $name;
      $renderer->doc .= '<applet code="'.$code.'"' . 
                               ' width="'.$width.'"'.
                              ' height="'.$height.'"' .
                             ' archive="'.$archive.'"'.
                             ' name="'.$bname.'">'.
                             '<param name="lbl" value="'.$lbl.'"/>'.
							'<param name="bgcolor" value="'.$bgcolor.'">'.
							'<param name="speed" value="'.$speed.'">'.
							'<param name="start" value="'.$start.'">'.
							'<param name="endless" value="'.$endless.'">'.
							'<param name="letters" value="de/dokutransdata/gms/letters"/>'.
                            '</applet>';
      return true;
    } else {
      return false;
    }
  }   
}
